#ifndef __flash_H
#define __flash_H

#ifdef APM32E10X_HD
#include "apm32e10x_fmc.h"
#elif defined AT32F403AVGT7
#include "at32f403a_407_flash.h"
#else
#include "apm32f10x_fmc.h"
#endif
#include "BoardData.h"


// 页大小
#if defined APM32F10X_HD || defined APM32E10X_HD || defined AT32F403AVGT7
#define FLASH_PAGE_SIZE    ((uint16_t)0x800)  // 2048B
#else
#define FLASH_PAGE_SIZE    ((uint16_t)0x400)
#endif

// 保存功率曲线多少页：1页 = 2048 / 16 = 128个;
#define LOG_FLASH_PAGES      2


#if defined APM32F10X_HD || defined APM32E10X_HD
#define Flash_Size         ((uint32_t)0x80000)
#elif defined AT32F403AVGT7
#define Flash_Size         ((uint32_t)0x100000)
#else
#define Flash_Size         ((uint32_t)0x20000)
#endif

// 普通参数保存地址: 最后一页。普通参数只保存1页
// 储存flash块1,起止地址；flash按页擦除;
#if defined APM32F10X_HD || defined APM32E10X_HD
#define BANK1_WRITE_START_ADDR  ((uint32_t)0x0807f800)
#elif defined AT32F403AVGT7
#define BANK1_WRITE_START_ADDR  ((uint32_t)0x080ff800)
#else
#define BANK1_WRITE_START_ADDR  ((uint32_t)0x0801f800)
#endif
// 参数保存页的结尾：
#if defined APM32F10X_HD || defined APM32E10X_HD
#define BANK1_WRITE_END_ADDR    ((uint32_t)0x0807ffff)
#elif defined AT32F403AVGT7
#define BANK1_WRITE_END_ADDR    ((uint32_t)0x080fffff)
#else
#define BANK1_WRITE_END_ADDR    ((uint32_t)0x0801ffff)
#endif

// 数据块页相关地址：
#if LOG_ON > 0 || CURVE_ON > 0  
	#if defined APM32F10X_HD || defined APM32E10X_HD
	#define Page1_END_ADDR      ((uint32_t)0x0807f7ff)
	#elif defined AT32F403AVGT7
	#define Page1_END_ADDR      ((uint32_t)0x080ff7ff)
	#else
	#define Page1_END_ADDR      ((uint32_t)0x0801f7ff)
	#endif 
#endif

// 日志保存相关
#if LOG_ON > 0
  // 日志保存的数量：
	#if defined APM32F10X_HD || defined APM32E10X_HD || defined AT32F403AVGT7
		// 每页能保存多少个日志数据
		#define Logs_Num_PerPage      170    // 单页数量 = 2048 / 12 = 170		
	#else
		#define Logs_Num_PerPage      85     // 单页数量 = 1024 / 12 = 85		
	#endif  
	
  // 保存多少页日志;
  #define  LOGS_FLASH_PAGES       2
	
#endif
	
// 功率曲线保存相关
#if CURVE_ON > 0	
  // 日志保存的数量：
	#if UI_Meter_Method == Meter_GuageIC_RN8302B  // 3相桩
	  #if defined APM32F10X_HD || defined APM32E10X_HD || defined AT32F403AVGT7
			#define Curv_Num_PerPage      85    // 单页数量 = 2048 / 24 = 85		
		#else
			#define Curv_Num_PerPage      42     // 单页数量 = 1024 / 24 = 42		
		#endif  
	#else
	  #if defined APM32F10X_HD || defined APM32E10X_HD || defined AT32F403AVGT7
			#define Curv_Num_PerPage      170    // 单页数量 = 2048 / 12 = 170		
		#else
			#define Curv_Num_PerPage      85     // 单页数量 = 1024 / 12 = 85		
		#endif
	#endif
		
  // 保存多少页日志;
  #define  Curv_FLASH_PAGES          2
	 
#endif	


// 日志保存的地址：从页开始
extern union flashDataUnion writeData[Write_Data_size];

void flash_rw_init(void);
void flash_write(void);
void flash_read(void);

#if LOG_ON > 0 && Nor_Flash == 0
void flash_log_write(void);
void flash_log_read(void);

#endif

#if CURVE_ON > 0 && Nor_Flash == 0
void flash_curv_write(void);
void flash_curv_read(void);

#endif

#endif
